package com.ossfk.jt808.server;

import com.ossfk.jt808.codec.Jt808Decoder;
import com.ossfk.jt808.codec.Jt808Encoder;
import com.ossfk.jt808.pojo.Jt808Msg;
import com.ossfk.jt808.server.adapter.Jt808ServerAdapter;
import com.ossfk.jt808.utils.Jt808Utils;
import com.ossfk.net.config.NetServerConfig;
import com.ossfk.net.server.AbstractTcpServer;
import com.ossfk.net.server.adapter.AbstractServerAdapter;
import com.ossfk.net.session.TcpSession;

import io.netty.handler.codec.ByteToMessageDecoder;
import io.netty.handler.codec.DelimiterBasedFrameDecoder;
import io.netty.handler.codec.MessageToByteEncoder;

/**
 * JT808服务端，遵循交通部808协议通讯
 * @ClassName: Jt808Server
 * @author Liujiefu
 * @date 2020-03-17 13:48:34
 */
public class Jt808Server extends AbstractTcpServer {
	
	public Jt808Server() {
		super();
	}

	public Jt808Server(NetServerConfig serverConfig) {
		super(serverConfig);
	}

	@Override
	public void sendMsg(TcpSession session, Object msg) {
		if (msg instanceof Jt808Msg) {
			Jt808Msg jt808Msg = (Jt808Msg) msg;
			if (session == null) {
				session = jt808Msg.getSession();
			}
			jt808Msg.getHeader().setFlowId(session.nextFlowId());
		}
		super.sendMsg(session, msg);
	}

	@Override
	protected DelimiterBasedFrameDecoder getFrameDecoder() {
		return Jt808Utils.getFrameDecoder();
	}

	@Override
	protected ByteToMessageDecoder getMsgDecoder() {
		return new Jt808Decoder();
	}

	@Override
	protected MessageToByteEncoder<?> getMsgEncoder() {
		return new Jt808Encoder();
	}

	@Override
	protected AbstractServerAdapter getChannelAdapter() {
		return new Jt808ServerAdapter();
	}
}
